import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:like_button/like_button.dart';
import 'package:niuren_demo/configs/App_style_config.dart';
import 'package:niuren_demo/configs/Colors_util.dart';
import 'package:niuren_demo/model/cDynamicPage_model.dart';
import 'package:niuren_demo/view/dynamic_page/photo.dart';
import 'package:niuren_demo/view/text/text_style.dart';

class cDynamicItem extends StatelessWidget {
  final cDynamicItemModel dataModel;

  const cDynamicItem({Key key, this.dataModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 8, vertical: 6),
            width: 24,
            height: 24,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(
                Radius.circular(2),
              ),
              image: DecorationImage(
                image: NetworkImage(
                  dataModel.imageurl ?? "",
                ),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Expanded(
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text_style(
                              data: dataModel.name,
                              fontSize: 14,
                            ),
                          ],
                        ),
                        Row(
                          children: [
                            Container(
                              color: AppStyleConfig.themColor,
                              padding: EdgeInsets.symmetric(
                                  horizontal: 2, vertical: 2),
                              child: Text_style(
                                data: dataModel.tip1,
                                color: Colors.white,
                                fontSize: 12,
                              ),
                            ),
                            SizedBox(
                              width: 4,
                            ),
                            Container(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 2, vertical: 2),
                              child: Text_style(
                                data: dataModel.tip2,
                                color: Colors.white,
                                fontSize: 12,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        Text_style(
                          data: dataModel.data,
                          color: Color(0xff8c8c8c),
                          fontSize: 14,
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Text_style(
                          data: dataModel.time,
                          color: Color(0xff8c8c8c),
                          fontSize: 14,
                        ),
                      ],
                    ),
                  ],
                ),
                Padding(
                  padding: EdgeInsets.symmetric(vertical: 8),
                  child: Text_style(
                    data: dataModel.content,
                    maxLines: 3,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                buildImages(imageUrl: dataModel.images),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    buildButton(
                        count: dataModel.likenum,
                        icon: Icons.wb_sunny,
                        isSelected: dataModel.isLike,
                        handle: (value) async {
                          print(value.toString());
                          if (value) {
                            dataModel.likenum -= 1;
                          } else {
                            dataModel.likenum += 1;
                          }
                          dataModel.isLike = !dataModel.isLike;
                          return dataModel.isLike;
                        }),
                    buildButton(
                        count: dataModel.commentnum,
                        icon: Icons.wb_sunny,
                        isSelected: false,
                        handle: (value) async {
                          return false;
                        }),
                    buildButton(
                        count: dataModel.focusnum,
                        icon: Icons.wb_sunny,
                        isSelected: dataModel.isFocus,
                        handle: (value) async {
                          print(value.toString());
                          if (value) {
                            dataModel.focusnum -= 1;
                          } else {
                            dataModel.focusnum += 1;
                          }
                          dataModel.isFocus = !dataModel.isFocus;
                          return dataModel.isFocus;
                        }),
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildImages({List<String> imageUrl}) {
    if (imageUrl == null || imageUrl.length == 0) return Container();
    return Container(
      margin: EdgeInsets.symmetric(vertical: 10),
      height: imageUrl.length == 1
          ? 200
          : imageUrl.length == 2
              ? 140
              : 100,
      child: GridView.builder(
          physics: NeverScrollableScrollPhysics(),
          itemCount: imageUrl.length >= 3 ? 3 : imageUrl.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: imageUrl.length >= 3 ? 3 : imageUrl.length,
            mainAxisSpacing: 5.0,
            crossAxisSpacing: 5.0,
          ),
          itemBuilder: (BuildContext context, int index) {
            //Widget Function(BuildContext context, int index)
            return GestureDetector(
              onTap: () {
                Navigator.of(context).push(FadeRoute(
                    page: PhotoPage(
                  images: imageUrl,
                  index: index,
                )));
              },
              child: buildImageItem(
                  imageUrl: imageUrl[index],
                  count: index < 2 ? 0 : imageUrl.length - 2),
            );
          }),
    );
  }

  Widget buildImageItem({String imageUrl, int count}) {
    return Stack(
      children: [
        Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
              image: DecorationImage(
            image: NetworkImage(
              imageUrl,
            ),
            fit: BoxFit.cover,
          )),
          child: Visibility(
            visible: count > 0,
            child: Text(
              "+$count",
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ),
        BackdropFilter(
            filter: new ImageFilter.blur(sigmaX: 0.5, sigmaY: 0.5),
            child: new Visibility(
              visible: count > 0,
              child: Container(
                color: Colors.black.withOpacity(0.4),
              ),
            )),
      ],
    );
  }

  Widget buildButton(
      {@required int count,
      @required IconData icon,
      bool isSelected = false,
      Future<bool> Function(bool) handle}) {
    return LikeButton(
        likeBuilder: (bool isLiked) {
          return Icon(
            icon,
            color: isLiked ? Colors.red : Colors.grey,
            size: 20,
          );
        },
        likeCountPadding: EdgeInsets.only(left: 4),
        countBuilder: (int likeCount, bool isLiked, String text) {
          return Text(
            text,
            style: TextStyle(color: Colors.grey, fontSize: 12),
          );
        },
        likeCount: count,
        isLiked: isSelected,
        onTap: handle);
  }
}

//跳转动画
class FadeRoute extends PageRouteBuilder {
  final Widget page;
  FadeRoute({this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) =>
              FadeTransition(
            opacity: animation,
            child: child,
          ),
        );
}
